package com.clouds.wms.dao;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import com.clouds.wms.bean.Purmf;
import com.clouds.wms.exception.DatabaseException;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;

@Repository("purmfDao")
public class PurmfDaoImpl implements PurmfDao {
	private Log log = LogFactory.getLog(getClass());
	@Resource(name="namedParameterJdbcTemplate")
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	@Override
	public PagesBean getPurmfList(String maaa, String maab, int offset,int pageSize) throws DatabaseException {
		PagesBean pb = new PagesBean();
		List<Purmf> list = new ArrayList<Purmf>();
		StringBuffer sql = new StringBuffer("SELECT * FROM purmf ");
		sql.append( " where mfaa = '" + Function.formatSQLString(maaa) + "'");
		sql.append( " and mfab = '" + Function.formatSQLString(maab) + "'");
		sql.append(" ORDER BY mfac ASC");
		try {
			int totalCount = namedParameterJdbcTemplate.getJdbcOperations().queryForInt(Function.toCountSql(sql.toString()));
			if(totalCount!=0){
				list = namedParameterJdbcTemplate.getJdbcOperations().query(Function.mySqlQuery(sql.toString(),offset,pageSize,totalCount),new BeanPropertyRowMapper<Purmf>(Purmf.class));
			}
			pb.setCount(totalCount);
			pb.setData(list);
			return pb;
		} catch (EmptyResultDataAccessException e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int getMaxMfac()throws DatabaseException {
		int mfac = namedParameterJdbcTemplate.getJdbcOperations().queryForInt("select IFNULL(max(mfac),0) mfac from purmf ");
		if(mfac==0){
			mfac = 10000;
		}else{
			mfac += 10000;
		}
		return mfac;
	}
	@Override
	public int savePurcb(Purmf purmf) throws DatabaseException {
		String sql = "INSERT INTO purmf (mfaa,mfab,mfac,mfad,mfae,mfaf,mfag,mfah,mfai,mfaj,mfak,mfal) VALUES (:mfaa,:mfab,:mfac,:mfad,:mfae,:mfaf,:mfag,:mfah,:mfai,:mfaj,:mfak,:mfal)";
		try {
			SqlParameterSource param = new BeanPropertySqlParameterSource(purmf);
			return namedParameterJdbcTemplate.update(sql, param);
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int delPurmf(String mfaa,String mfab,String mfacs) throws DatabaseException {
		String sql = "DELETE FROM purmf WHERE mfaa=? and mfab=? and mfac in ("+mfacs+")";
		try {
			return namedParameterJdbcTemplate.getJdbcOperations().update(sql, new Object[]{mfaa,mfab});
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int updatePurcb(Purmf purmf) throws DatabaseException {
		String sql = "UPDATE purmf SET mfad=:mfad, mfae=:mfae,mfaf=:mfaf,mfag=:mfag,mfah=:mfah,mfai=:mfai,mfaj=:mfaj, mfak=:mfak,mfal=:mfal WHERE mfaa=:mfaa and mfab = :mfab and mfac =:mfac";
		try {
			SqlParameterSource param = new BeanPropertySqlParameterSource(purmf);
			return namedParameterJdbcTemplate.update(sql, param);
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int changeStatus(String mfaa, String mfab, String status)throws DatabaseException {
		String sql = "UPDATE purme SET meac=? WHERE meaa=? and meab = ?";
		try {
			return namedParameterJdbcTemplate.getJdbcOperations().update(sql, new Object[]{status,mfaa,mfab});
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public void delPurmfByAaAb(String maaa, String maab) throws DatabaseException {

		String sql2 = "delete from purmf where mfaa = ? and mfab = ?";
		namedParameterJdbcTemplate.getJdbcOperations().update(sql2, new Object[]{maaa,maab});
		
		
	}
}
